package com.bai.Controller;

import com.bai.Pojo.Post;
import com.bai.Service.PostService;
import com.bai.Utils.DtoResultUtil;
import com.bai.dto.PageLimitDto;
import com.bai.dto.PublicResponseBody;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Post")
public class PostController {
    @Autowired
    private PostService postService;

    //查询所有员工
    @GetMapping("/Post_table")
    public PageLimitDto QueryAllPost(Integer offset, Integer limit){
        PageLimitDto result =postService.QueryAll(offset,limit);
        return result;
    }

    //添加员工
    @RequiresRoles(value={"admin"})
    @GetMapping("/add")
    public PublicResponseBody add(Post post){
        PublicResponseBody addPost = postService.addPost(post);
        if (addPost!=null){
            if (addPost==null){
                return DtoResultUtil.resultError();
            }
            return addPost;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }


    //删除
    @RequiresRoles(value={"admin"})
    @GetMapping("/delete")
    public PublicResponseBody delete(Integer id){
        if (id!=null){
            PublicResponseBody result = postService.deletePostById(id);
            if (result==null){
                return DtoResultUtil.resultError();
            }
            return result;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }

    //编辑修改
    @RequiresRoles(value={"admin"})
    @PostMapping("/edit")
    public PublicResponseBody edit(Post post){
        PublicResponseBody updatePost = postService.UpdatePost(post);
        if (updatePost!=null){
            if (updatePost==null){
                return DtoResultUtil.resultError();
            }
            return updatePost;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }


    @GetMapping("/getDetail")
    public PublicResponseBody getDetail(Integer id){
        if (id!=null){
            PublicResponseBody getPost= postService.getPost(id);
            if (getPost!=null){
                if (getPost==null){
                    return DtoResultUtil.resultError();
                }
                return getPost;
            }else {
                return DtoResultUtil.resultParamError();
            }
        }
        return DtoResultUtil.resultParamError();
    }
}
